package com.zat.mes.andrmes.dofun.work

import android.content.Intent
import android.view.View
import android.widget.RelativeLayout
import android.widget.TextView
import androidx.core.content.ContextCompat
import androidx.lifecycle.ViewModelProvider
import androidx.recyclerview.widget.LinearLayoutManager
import com.bigkoo.pickerview.builder.TimePickerBuilder
import com.bigkoo.pickerview.listener.OnTimeSelectListener
import com.bigkoo.pickerview.view.TimePickerView
import com.chad.library.adapter.base.listener.OnLoadMoreListener
import com.contrarywind.view.WheelView
import com.google.gson.Gson
import com.zat.mes.andrmes.R
import com.zat.mes.andrmes.base.BaseActivity
import com.zat.mes.andrmes.data.LoginResultBean
import com.zat.mes.andrmes.data.WorkWageBean
import com.zat.mes.andrmes.dofun.work.adapter.WorkWageAdapter
import com.zat.mes.andrmes.dofun.work.vm.WageModel
import com.zat.mes.andrmes.global.CompanionType
import com.zat.mes.andrmes.utils.DateUtil
import com.zat.mes.andrmes.utils.DisplayUtil
import com.zat.mes.andrmes.utils.GlideUtil
import com.zat.mes.andrmes.utils.PreferencesUtil
import kotlinx.android.synthetic.main.activity_work_wage.*
import kotlinx.android.synthetic.main.layout_title.*
import java.util.*

/**
 * 工资查看
 */
class WorkWageActivity : BaseActivity() {

    private var startPicker: TimePickerView? = null
    var startDate: String? = DateUtil.formatDate(Date(), DateUtil.FORMAT_3)
    private var endPicker: TimePickerView? = null
    var endDate: String? = DateUtil.formatDate(Date(), DateUtil.FORMAT_3)
    var empId: Long = 0
    private var currentPage = 1
    private var wageModel: WageModel? = null

    override fun init() {
        initView()
        initData()
        addListener()
        requestData()

    }

    /**
     * 添加点击监听
     */
    private fun addListener() {
        btnStartDateWage.setOnClickListener {
            startPicker?.show()
        }
        btnEndDateWage.setOnClickListener { endPicker?.show() }
    }

    /**
     * 请求数据
     */
    private fun requestData() {
        wageModel?.loadWage(currentPage, 100, empId, startDate, endDate)
    }

    /**
     * 初始化数据
     */
    private fun initData() {
        val loginResultBean = Gson().fromJson(
            PreferencesUtil.getString(CompanionType.LOGIN_RESULT),
            LoginResultBean::class.java
        )
        empId = loginResultBean.empid
        GlideUtil.setHeader(this, loginResultBean?.img, ivHeadWage)
        tvNameWage.text = loginResultBean.empname
        tvStartDateWage.text = DateUtil.formatDate(Date(), DateUtil.FORMAT_z)
        tvEndDateWage.text = DateUtil.formatDate(Date(), DateUtil.FORMAT_z)
        initDatePick()

        recyclerWage.layoutManager = LinearLayoutManager(GetPackActivity@ this)
        val workWageAdapter = WorkWageAdapter(R.layout.item_work_wage)
        recyclerWage.adapter = workWageAdapter

        val loadMoreModule = workWageAdapter.loadMoreModule
        // 当数据不满一页时，是否继续自动加载（默认为true）
        loadMoreModule?.isEnableLoadMoreIfNotFullPage = false
        loadMoreModule?.isAutoLoadMore = false
        loadMoreModule?.setOnLoadMoreListener(OnLoadMoreListener {
            requestData()
        })


        wageModel = ViewModelProvider(this).get(WageModel::class.java)
        wageModel?.getWage()?.observe(this, androidx.lifecycle.Observer {
            closeLoading()
            if (it != null) {
                val result = it.result
                if (result != null) {
                    if (result.items != null) {
                        if (currentPage == 1) {
                            if (result.items.size <= 0) {
                                setEmptyVisible()
                            } else {
                                setEmptyGone()
                            }
                            workWageAdapter.setNewData(result.items as MutableList<WorkWageBean>)
                        } else {
                            workWageAdapter.addData(result.items as MutableList<WorkWageBean>)
                        }
                    }
                    if (result.currentPage == result.totalPage) {
                        loadMoreModule?.loadMoreEnd()
                    } else {
                        loadMoreModule?.loadMoreComplete()
                        currentPage++
                    }
                } else {
                    loadMoreModule?.loadMoreEnd()
                }
            } else {
                loadMoreModule?.loadMoreEnd()
            }
        })
        wageModel?.getWageFail()?.observe(this, androidx.lifecycle.Observer {
            closeLoading()
        })

    }

    /**
     * 初始化view
     */
    private fun initView() {
        setContentView(R.layout.activity_work_wage)
        rlReturnTitle.visibility = View.VISIBLE
        rlReturnTitle.setOnClickListener { finish() }
        tVtitle.text = getString(R.string.title_work_wage)
        val textView = TextView(WorkWageActivity@ this)
        textView.text = getString(R.string.text_month_all)
        textView.setTextColor(ContextCompat.getColor(baseContext, R.color.color_white))
        rlRightComm.addView(textView)
        rlRightComm.visibility = View.GONE
        val layoutParams: RelativeLayout.LayoutParams =
            textView.layoutParams as RelativeLayout.LayoutParams
        layoutParams.addRule(RelativeLayout.CENTER_IN_PARENT, RelativeLayout.TRUE)
        layoutParams.rightMargin = DisplayUtil.dip2px(this, 30f)
        rlRightComm.setOnClickListener {
            startBaseActivity(Intent(WorkWageActivity@ this, MonthWageActivity::class.java))
        }
        showLoading()
    }

    /**
     * 初始化时间选择器
     */
    private fun initDatePick() {
        startPicker = TimePickerBuilder(WorkWageActivity@ this, object : OnTimeSelectListener {
            override fun onTimeSelect(date: Date?, v: View?) {
                if (date != null) {
                    startDate = DateUtil.formatDate(date, DateUtil.FORMAT_3)
                    tvStartDateWage.text = DateUtil.formatDate(date, DateUtil.FORMAT_z)
                    currentPage = 1
                    requestData()
                    showLoading()

                }
            }

        }).setSubmitColor(ContextCompat.getColor(WorkWageActivity@ this, R.color.color_text_nomal))
            .setCancelColor(
                ContextCompat.getColor(
                    WorkWageActivity@ this,
                    R.color.color_text_nomal
                )
            )
            .setTitleBgColor(ContextCompat.getColor(WorkWageActivity@ this, R.color.color_white))
            .setContentTextSize(18)
            .setLabel("", "", "", "", "", "")
            .setDividerType(WheelView.DividerType.WRAP)
            .setDividerColor(ContextCompat.getColor(WorkWageActivity@ this, R.color.color_theme))
            .setLineSpacingMultiplier(2.0f)
            .build()

        endPicker = TimePickerBuilder(WorkWageActivity@ this, object : OnTimeSelectListener {
            override fun onTimeSelect(date: Date?, v: View?) {
                if (date != null) {
                    endDate = DateUtil.formatDate(date, DateUtil.FORMAT_3)
                    tvEndDateWage.text = DateUtil.formatDate(date, DateUtil.FORMAT_z)
                    currentPage = 1
                    requestData()
                    showLoading()

                }
            }

        }).setSubmitColor(ContextCompat.getColor(WorkWageActivity@ this, R.color.color_text_nomal))
            .setCancelColor(
                ContextCompat.getColor(
                    WorkWageActivity@ this,
                    R.color.color_text_nomal
                )
            )
            .setTitleBgColor(ContextCompat.getColor(WorkWageActivity@ this, R.color.color_white))
            .setContentTextSize(18)
            .setLabel("", "", "", "", "", "")
            .setDividerType(WheelView.DividerType.WRAP)
            .setDividerColor(ContextCompat.getColor(WorkWageActivity@ this, R.color.color_theme))
            .setLineSpacingMultiplier(2.0f)
            .build()
    }

}
